const data = [
    {
        title: '网络谜踪',
        rate: '8.7',
        url: 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2528018780.jpg'
    },
    {
        title: '一出好戏',
        rate: '7.2',
        url: 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2529571873.jpg'
    },
    {
        title: '幸福的拉扎罗',
        rate: '8.7',
        url: 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2521583093.jpg'
    },
    {
        title: '协商',
        rate: '6.9',
        url: 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2530394864.jpg'
    }];

const mysql = require('mysql');

//  创建连接
const connection = mysql.createConnection({
host : 'localhost',
port : '8889',
user : 'root',
password : 'root',
database : 'movie_douban'
});
//  执行创建连接
connection.connect();

//  SQL查询语句
// const findData = "SELECT * FROM `movie_list`";
//
// connection.query(findData, (err, res) => {
//     if (err) {
//         console.log(err)
//     } else {
//         console.log(res)
//     }
// });

// 构造要写入的数据
const insertData = [];

for (let i = 0; i < data.length; i++) {
    const el = data[i];
    const _data = [el.rate, el.title, el.url];
    insertData.push(_data);
}

// console.log(insertData);

//  插入单条数据
// const sql = "INSERT INTO movie_list (`rate`, `title`, `url`) VALUES ('test', '2', '3')";

// 插入多条数据
const sql = "INSERT INTO movie_list (`rate`, `title`, `url`) VALUES ?";

connection.query(sql, [insertData], (err, res) => {
   if (err) {
       console.log(err);
   } else {
       console.log('INSERT SUCCESS')
   }

});
// 断开
connection.end();

// 执行完结束进程
// process.exit(0);
